//
//  AppDelegate.m
//  EncryptionTPB
//
//  Created by LvJianfeng on 15/9/11.
//  Copyright © 2015年 coderjianfeng. All rights reserved.
//

#import "AppDelegate.h"
#import "JFUtil.h"
#import "TPBLaunchView.h"
#import "AddressTableViewController.h"
#import "TPBInitialize.h"
#import "TPBCallViewController.h"

@interface AppDelegate ()
@property (strong, nonatomic) TPBLaunchView *launchView;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //init
    TPBInitialize *initialize = [TPBInitialize sharedManager];
    [initialize checkDBExistsAtPathWithSTPB];
    
    self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.window makeKeyAndVisible];
    
    [self initRootTabBarController];
    
    self.window.rootViewController = self.tabBarController;
    
    self.launchView = [[TPBLaunchView alloc] initWithFrame:self.window.bounds];
    
    [self.window addSubview:self.launchView];
    [self.window bringSubviewToFront:self.launchView];
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(removeLaunchView) userInfo:nil repeats:NO];
    
    return YES;
}

/**
 *  动画移除view
 */
-(void)removeLaunchView
{
    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.launchView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.launchView removeFromSuperview];
    }];
}

/**
 *  初始化根tabbar
 */
- (void)initRootTabBarController{
    [[UITabBar appearance] setBackgroundImage:[JFUtil imageWithColor:[UIColor whiteColor] size:CGSizeMake(SCREEN_WIDTH, 49)]];
    //设置全局导航条样式
    [JFUtil setNavigationAppearanceBackImage:[UIImage imageNamed:@"back"]];
    [JFUtil hideBackTitle:YES];
    //[JFUtil setNavigationAppearance:[UIColor whiteColor] tintColor:[UIColor customLightPlaceColor] backgroundColor:[UIColor whiteColor] translucent:NO hideBackTitle:YES];
    //通讯录
    AddressTableViewController *addressTableVC = [[AddressTableViewController alloc] init];
    UINavigationController *addressTableNav = [[UINavigationController alloc] initWithRootViewController:addressTableVC];
    [JFUtil setNavigationTitleStyle:addressTableNav titleColor:[UIColor customGrayColor] titleFont:[UIFont systemFontOfSize:16]];
    //拨号盘
    TPBCallViewController *callVC = [[TPBCallViewController alloc] init];
    UINavigationController *callNav = [[UINavigationController alloc] initWithRootViewController:callVC];
    [JFUtil setNavigationTitleStyle:callNav titleColor:[UIColor customGrayColor] titleFont:[UIFont systemFontOfSize:16]];
    
    NSArray *viewControllers = [[NSArray alloc] initWithObjects:callNav,addressTableNav, nil];
    self.tabBarController = [[UITabBarController alloc] init];
    //self.tabBarController.tabBar.tintColor = [UIColor customSystemColor];
    self.tabBarController.delegate = self;
    self.tabBarController.viewControllers = viewControllers;
    self.tabBarController.selectedIndex = 0;
    
    UITabBarItem *callItem = [[UITabBarItem alloc] initWithTitle:@"拨号" image:[UIImage imageNamed:@"ongoing_unselected"] selectedImage:[UIImage imageNamed:@"ongoing_selected"]];
    callNav.tabBarItem = callItem;
    
    UITabBarItem *addressItem = [[UITabBarItem alloc] initWithTitle:@"电话簿" image:[UIImage imageNamed:@"ongoing_unselected"] selectedImage:[UIImage imageNamed:@"ongoing_selected"]];
    addressTableNav.tabBarItem = addressItem;
}
/*
 [[UITabBar appearance] setBackgroundImage:tabBarBackground];//设置背景，修改颜色是没有用的
 
 [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tm.png"]];
 
 [[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"tm.png"]];//隐藏那条黑线
 
 
 
 [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
 
 
 
 UIColor *titleHighlightedColor = [UIColor colorWithRed:198/255.0 green:59/255.0 blue:82/255.0 alpha:1.0];
 
 
 
 [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleHighlightedColor, UITextAttributeTextColor,nil] forState:UIControlStateHighlighted];
 */

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
